package com.twu.biblioteca;

import java.util.Scanner;

class Menu{

    Library library = new Library();
    Customer currentCustomer;

    public void menuItems(){
        System.out.print(menuList());
        promptForMenuItem();
    }

    public String menuList(){
        return ("To select an item, please type in the number and press enter:\n" +
        "(1) Checkout a Book\n" +
        "(2) Checkout a Movie\n" +
        "(3) Return a Book\n" +
        "(4) Return a Movie\n" +
        "(5) List Books and Movies\n" +
        "(6) List Books\n" +
        "(7) List Movies\n" +
        "(8) View Account Information\n" +
        "(9) Exit\n");
    }

    public void menuSelection(int num){
        if (num == 1){
            checkForCurrentCustomer();
            promptForBookRelease();
            promptForMenuItem();
        } else if (num == 2){
            checkForCurrentCustomer();
            promptForMovieRelease();
            promptForMenuItem();
        } else if (num == 3){
            checkForCurrentCustomer();
            promptForBookReturn();
            promptForMenuItem();
        } else if (num == 4){
            checkForCurrentCustomer();
            promptForMovieReturn();
            promptForMenuItem();
        } else if (num == 5){
            listBooksAndMoviesAndMenu();
        } else if (num == 6){
            listBooksAndMenu();
        } else if (num == 7){
            listMoviesAndMenu();
        } else if (num == 8){
            checkForCurrentCustomer();
            viewAccountInformation();
        } else if (num == 9){
            clearCurrentCustomer();
            goodbyeMessage();
        } else {
            System.out.println("Select a valid option!");
            promptForMenuItem();
        }
    }

    public void promptForMenuItem(){
        prompt();
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        menuSelection(num);
    }

    public void promptForBookRelease(){
        System.out.println("Please enter book title you would like to check out:");
        prompt();
        Scanner sc = new Scanner(System.in);
        String title = sc.nextLine();
        checkReleaseBook(title);
    }

    public void promptForBookReturn(){
        System.out.println("Please enter book title you would like to return:");
        prompt();
        Scanner sc = new Scanner(System.in);
        String title = sc.nextLine();
        checkReturnBook(title);
    }

    public void checkReturnBook(String title){
        if (!library.bookSection.bookIsValidForReturn(title)){
            System.out.println("This is not a valid book to return.");
        } else if (!currentCustomer.hasBook(title)){
            System.out.println("You do not have that book to return");
        } else {
            returnBook(title);
        }
    }

    public void returnBook(String title){
        currentCustomer.returnBook(title);
        library.bookSection.acceptBook(title);
        library.bookSection.selectBook(title).owner = null;
        System.out.println("Thank you for returning the book");
    }

    public void checkReleaseBook(String title){
        if (library.bookSection.bookIsValidForRelease(title)){
            releaseBook(title);
        } else {
            System.out.println("That book is not available.");
        }
    }

    public void releaseBook(String title){
        library.bookSection.releaseBook(title);
        currentCustomer.checkoutBook(title);
        library.bookSection.selectBook(title).owner = currentCustomer;
        System.out.println("Thank you! Enjoy the book");
    }

    public void listBooksAndMenu(){
        library.bookSection.printAvailableBooks();
        menuItems();
    }

    public void promptForMovieRelease(){
        System.out.println("Please enter movie title you would like to check out:");
        prompt();
        Scanner sc = new Scanner(System.in);
        String title = sc.nextLine();
        checkReleaseMovie(title);
    }

    public void promptForMovieReturn(){
        System.out.println("Please enter movie title you would like to return:");
        prompt();
        Scanner sc = new Scanner(System.in);
        String title = sc.nextLine();
        checkReturnMovie(title);
    }

    public void checkReturnMovie(String title){
        if (!library.movieSection.movieIsValidForReturn(title)){
            System.out.println("This is not a valid movie to return.");
        } else if (!currentCustomer.hasMovie(title)){
            System.out.println("You do not have that movie to return");
        } else {
            returnMovie(title);
        }
    }

    public void returnMovie(String title){
        currentCustomer.returnMovie(title);
        library.movieSection.acceptMovie(title);
        library.movieSection.selectMovie(title).owner = null;
        System.out.println("Thank you for returning the movie");
    }

    public void checkReleaseMovie(String title){
        if (library.movieSection.movieIsValidForRelease(title)){
            releaseMovie(title);
        } else {
            System.out.println("That movie is not available.");
        }
    }

    public void releaseMovie(String title){
        library.movieSection.releaseMovie(title);
        currentCustomer.checkoutBook(title);
        library.movieSection.selectMovie(title).owner = currentCustomer;
        System.out.println("Thank you! Enjoy the movie");
    }

    public void listMoviesAndMenu(){
        library.movieSection.printAvailableMovies();
        menuItems();
    }

    public void listBooksAndMoviesAndMenu(){
        library.bookSection.printAvailableBooks();
        library.movieSection.printAvailableMovies();
        menuItems();
    }

    public void goodbyeMessage(){
        System.out.println("Goodbye");
    }

    private void prompt(){
        System.out.print("> ");
    }

    public void promptForLogin(){
        System.out.println("Please enter your 7-digit library number in the form xxx-xxxx");
        prompt();
        Scanner sc = new Scanner(System.in);
        String libraryNumber = sc.nextLine();
        validate(libraryNumber);
    }

    public void validate(String libraryNumber){
        if (selectCustomer(libraryNumber) == null){
            System.out.println("Incorrect number");
            menuItems();
        } else {
            System.out.println("Account verified");
            setCurrentCustomer(libraryNumber);
        }
    }

    public void setCurrentCustomer(String libraryNumber){
        currentCustomer = selectCustomer(libraryNumber);
    }

    public Customer selectCustomer(String libraryNumber){
        for (Customer customer : library.customers){
            if ((customer.libraryNumber).equals(libraryNumber))
                return customer;
        }
        return null;
    }

    public void viewAccountInformation(){
        System.out.println(currentCustomer.userInformation());
    }

    public void checkForCurrentCustomer(){
        if (currentCustomer == null) {
            promptForLogin();
        }
    }

    public void clearCurrentCustomer(){
        currentCustomer = null;
    }
}